package com.accompany.fragment.my.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.accompany.R;
import com.accompany.adapter.BaseRecyclerHolder;
import com.accompany.adapter.BaseRecyclerRefreshAdapter;
import com.accompany.api.bean.AddCardBean;
import com.accompany.api.category.BankCarCategory;
import com.accompany.api.presenter.BankCarPresenter;
import com.accompany.base.BaseActivity;
import com.accompany.data.YuyueKeshiDataUtils;
import com.accompany.dialog.LoginOutDialog;
import com.accompany.fragment.login.UserLoginActivity;
import com.accompany.main.ConstantStaticUrl;
import com.accompany.utils.BankCardUtils;
import com.accompany.utils.FullScreenUtils;
import com.accompany.utils.SPUtils;
import com.accompany.utils.ToastUtil;
import com.accompany.view.RecyclerViewLayoutManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 信息说明: 我的卡包
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/3/4 16 ：31
 * 项目名称：yuanban
 */
public class MyCardActivity extends BaseActivity implements BaseRecyclerRefreshAdapter.OnItemClickListener, BankCarCategory.View, OnRefreshListener{
    @BindView(R.id.base_title)
    TextView mTitleMsg;
    @BindView(R.id.base_refresh_recycler)
    RecyclerView mRecylerViews;
    @BindView(R.id.relat_empty)
    RelativeLayout mEmptyLayout;
    @BindView(R.id.base_refresh)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.icn_image)
    ImageView mIconIage;
    private BankCarPresenter mPresenter;
    private String mToken;
    private long mUserId;
    private BaseRecyclerRefreshAdapter<AddCardBean.AddCardBeanRows> mAdapter;
    private List<AddCardBean.AddCardBeanRows> mlist = new ArrayList<>();
    private String mStateTyped;
    private boolean isLoading =false;
    private int pageNumber = 1;
    private LoginOutDialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_card_view;
    }
    @Override
    public void initLayoutView() {
        mTitleMsg.setText("卡包");
        mStateTyped = getIntent().getStringExtra("states");
        FullScreenUtils.getInstance().fullScreen(MyCardActivity.this, true);
        RecyclerViewLayoutManager.setLinearVerticalManager(MyCardActivity.this, mRecylerViews);
        mAdapter = new BaseRecyclerRefreshAdapter<AddCardBean.AddCardBeanRows>(MyCardActivity.this, mlist, R.layout.card_item_view) {
            @Override
            public void convert(BaseRecyclerHolder holder, AddCardBean.AddCardBeanRows item, int position, boolean isScrolling) throws IOException {
                holder.setText(R.id.card_name, item.getBankName());
                holder.setText(R.id.card_states,item.getBankDTO().getCardType());
//                holder.setText(R.id.bank_card,item.getBankcardNo().replaceAll("\\w(?=\\w{4})", "*"));
                holder.setText(R.id.bank_card, BankCardUtils.handleBankCardNum(item.getBankcardNo()));
                ImageView imageView = holder.getView(R.id.image_card);
                RequestOptions requestOptions = new RequestOptions();
                String logo = "https://apimg.alipay.com/combo.png?d=cashier&t="+item.getBankDTO().getBank();
                requestOptions.error(R.drawable.logo_circler_bg)
                        .circleCrop()
                        .skipMemoryCache(true)
                        .diskCacheStrategy( DiskCacheStrategy.NONE );
//                Glide.with(MyCardActivity.this)
//                        .load(logo)
//                        .apply(requestOptions)
//                        .into(imageView);
            }
        };
        mAdapter.setDatas(mlist);
        mRecylerViews.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
        mPresenter = new BankCarPresenter(this);
        SetPersenter(mPresenter);
        mToken = (String) SPUtils.get(MyCardActivity.this, ConstantStaticUrl.mToken, "");
        SharedPreferences sharedPreferences = MyCardActivity.this.getSharedPreferences("First", MODE_PRIVATE);
        mUserId = sharedPreferences.getLong(ConstantStaticUrl.userId, 0);
        mPresenter.getBankCardlist(mToken, mUserId);
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setEnableLoadMore(false);

    }

    @OnClick({R.id.base_back, R.id.add_card})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finishWitchAnimation();
                break;
            case R.id.add_card:
                openActivityWitchAnimation(AddBlankCardActivity.class);
                break;
        }
    }
    @Override
    public void getBankCardlist(AddCardBean baseBean) {
        if (baseBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            if (baseBean.getRows() != null && baseBean.getRows().size() > 0) {
                if (pageNumber == 1) {
                    if (isLoading) {
                        isLoading = false;
                    } else {
                        mlist.clear();
                    }
                    mAdapter.setDatas(baseBean.getRows());
                } else {
                    mAdapter.loadMore(mlist);
                }
                mEmptyLayout.setVisibility(View.GONE);
                mRecylerViews.setVisibility(View.VISIBLE);
            } else {
                if (isLoading) {
                    isLoading = false;
                } else {
                    mEmptyLayout.setVisibility(View.VISIBLE);
                    mIconIage.setBackgroundResource(R.drawable.no_card_bg);
                    mRecylerViews.setVisibility(View.GONE);
                }
            }
        } else if (baseBean.getCode() == ConstantStaticUrl.NO_LOGIN_CODE) {//未登录
            dialog = LoginOutDialog.getLoginOutDialog(MyCardActivity.this);
            dialog.show();
            dialog.findViewById(R.id.circler_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    dialog = null;
                }
            });
            dialog.findViewById(R.id.to_login).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openActivityWitchAnimation(UserLoginActivity.class);
                    SPUtils.clear(MyCardActivity.this);
                    finish();
                    dialog.dismiss();
                    dialog = null;
                }
            });

        }else {
                ToastUtil.showToastINFO(MyCardActivity.this, baseBean.getMsg());
        }
    }
    @Override
    public void onItemClick(RecyclerView parent, View view, int position) {
        if (mStateTyped!=null&&mStateTyped.equals("0")){
            Intent intent = new Intent();
            //把返回数据存入Intent
            intent.putExtra("bank",mlist.get(position).getBankDTO().getBankName());
            intent.putExtra("bankCard",mlist.get(position).getBankcardNo());
            intent.putExtra("bankAddress",mlist.get(position).getBankcardOpenAddr());
            //设置返回数据
            MyCardActivity.this.setResult(200, intent);//RESULT_OK为自定义常量
            //关闭Activity
            MyCardActivity.this.finish();
        }else {
            Bundle bundle = new Bundle();
            bundle.putString("bankName",mlist.get(position).getBankDTO().getBankName());
            bundle.putString("bankCard",mlist.get(position).getBankcardNo());
            bundle.putString("id",mlist.get(position).getId());
            openActivityWitchAnimation(UnBindCardActivity.class,bundle);
        }
    }
    @Override
    public void SetPersenter(Object persenter) {

    }
    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(MyCardActivity.this,mErrorMsg);

    }

    @Override
    protected void onResume() {
        super.onResume();
        pageNumber = 1;
        mPresenter.getBankCardlist(mToken, mUserId);
        smartRefreshLayout.finishRefresh(500);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNumber = 1;
        mPresenter.getBankCardlist(mToken, mUserId);
        smartRefreshLayout.finishRefresh(500);
    }
}
